package com.centling.smartSealForPhone.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;

import com.centling.smartSealForPhone.R;
import com.centling.smartSealForPhone.adapter.AuditorSealManagerAdapter;
import com.centling.smartSealForPhone.bean.AuditorSealBean;
import com.centling.smartSealForPhone.constants.UrlConstants;
import com.centling.smartSealForPhone.utils.OkHttpUtil;
import com.centling.smartSealForPhone.utils.ShowToast;
import com.centling.smartSealForPhone.widget.AutoRecyclerView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;


/**
 *
 * @author Jessie
 * @date 2016/9/5
 */
public class AuditorSearchResultActivity extends TitleBarActivity {

    private AutoRecyclerView searchRecyclerView;
    private PtrClassicFrameLayout ptrClassicFrameLayout;
    private int pageNo = 0;
    private List<AuditorSealBean> auditorSealSearchListBean = new ArrayList<>();
    private Intent data;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        data = getIntent();
        switch (data.getStringExtra("isFirst")) {
            case "0":
                setTitleBarText("审批");
                break;
            case "1":
                setTitleBarText("复审");
                break;
            default:
                setTitleBarText("查询结果");
                break;
        }

        setContentView(R.layout.activity_auditor_seal_manager);
        initView();
    }

    private void initView() {
        searchRecyclerView = (AutoRecyclerView) findViewById(R.id.rv_auditor_seal);
        ptrClassicFrameLayout = (PtrClassicFrameLayout) findViewById(R.id.ptr_auditor_seal);
        AuditorSealManagerAdapter adapter = new AuditorSealManagerAdapter(mContext,
                auditorSealSearchListBean);
        searchRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 1));
        searchRecyclerView.setAdapter(adapter);
        ptrClassicFrameLayout.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout ptrFrameLayout) {
                sendRequest(true);
            }
        });
        searchRecyclerView.setLoadDataListener(new AutoRecyclerView.LoadDataListener() {
            @Override
            public void onLoadMore() {
                sendRequest(false);
            }
        });
    }

    private void sendRequest(final boolean isRefresh) {
        Map<String, String> params = new HashMap<>();
        params.put("applicantUserName", data.getStringExtra("applicantUserName"));
        params.put("sealName", data.getStringExtra("sealName"));
        params.put("status", data.getStringExtra("status"));
        params.put("isValid", data.getStringExtra("isValid"));
        params.put("applyReasonType", data.getStringExtra("applyReasonType"));
        params.put("applyType", data.getStringExtra("applyType"));
        params.put("applyTimeStart", data.getStringExtra("applyTimeStart"));
        params.put("applyTimeEnd", data.getStringExtra("applyTimeEnd"));
        params.put("authTimeStart", data.getStringExtra("authTimeStart"));
        params.put("authTimeEnd", data.getStringExtra("authTimeEnd"));
        params.put("reviewTimeStart", data.getStringExtra("reviewTimeStart"));
        params.put("reviewTimeEnd", data.getStringExtra("reviewTimeEnd"));
        if (isRefresh) {
            pageNo = 0;
        }

        postJsonEnqueue(UrlConstants.SEARCH_CHECK + pageNo, params, new OkHttpUtil.NetCallBack() {

            @Override
            public void onSucceed(String json) {
                if (isRefresh) {
                    auditorSealSearchListBean.clear();
                    ptrClassicFrameLayout.refreshComplete();
                    searchRecyclerView.setLoadDataListener(new AutoRecyclerView.LoadDataListener() {
                        @Override
                        public void onLoadMore() {
                            sendRequest(false);
                        }
                    });
                }
                JSONArray data;
                try {
                    data = new JSONObject(json).getJSONArray("rows");
                    List<AuditorSealBean> auditorSealSearchListBeanTemp;

                    auditorSealSearchListBeanTemp = new Gson().fromJson(data.toString(), new TypeToken<ArrayList<AuditorSealBean>>() {
                    }.getType());
                    auditorSealSearchListBean.addAll(auditorSealSearchListBeanTemp);
                    pageNo++;
                    searchRecyclerView.getAdapter().notifyDataSetChanged();

                    if (auditorSealSearchListBeanTemp.size() < 20) {
                        searchRecyclerView.loadMoreComplete(true);
                    } else {
                        searchRecyclerView.loadMoreComplete(false);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailed(String reason) {
                searchRecyclerView.loadMoreComplete(false);
                ptrClassicFrameLayout.refreshComplete();
                ShowToast.shortTime(reason);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        searchRecyclerView.post(new Runnable() {
            @Override
            public void run() {
                ptrClassicFrameLayout.autoRefresh();
            }
        });
    }
}
